//
//  KindViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
#import "KindViewController.h"
#import "KindCollectionViewCell.h"
#import "PrefixHeader.pch"
#import "KindModel.h"
#import "SearchViewController.h"
#import "DetailsViewController.h"
@interface KindViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>

@property(nonatomic, strong)NSMutableArray *arr;
@property(nonatomic, strong)NSMutableDictionary *dic;
@property(nonatomic, strong)UISearchBar *searchBar;// 搜索
@property(nonatomic,retain)UICollectionView *collectionView;
@end

@implementation KindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 一个section距离边缘上下左右的距离
    flowLayout.minimumInteritemSpacing = 12;
    flowLayout.minimumLineSpacing = 12;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width-78)/3, 150);

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height -49) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[KindCollectionViewCell class] forCellWithReuseIdentifier:@"kind"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
#pragma mark 创建搜索
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 25, self.view.bounds.size.width, 40)];
    self.searchBar.placeholder = @"输入漫画名或作者名";
    [self.searchBar setKeyboardType: UIKeyboardTypeNamePhonePad];// 键盘类型
    self.searchBar.delegate = self;
    self.navigationItem.titleView = self.searchBar;
    
    [self json];
}

#pragma mark 开始搜索时 创建取消按钮
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
#pragma mark 取消按钮时执行的方法
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.searchBar setShowsCancelButton:NO animated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
#pragma mark 键盘中 搜索按钮按下时 执行的方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%@",@"点击");
    [self.searchBar resignFirstResponder];
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    searchBar.text = [searchBar.text stringByAddingPercentEncodingWithAllowedCharacters:[ NSCharacterSet URLFragmentAllowedCharacterSet]];
    NSLog(@"%@",searchBar.text);
    searchVC.text = searchBar.text;
    [self.navigationController pushViewController:searchVC animated:YES];
    self.searchBar.text = nil;
    
}

-(void)json
{
    NSString *str = @"http://mobilev3.ac.qq.com/Classify/comicClassifyListV2ForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/943AAD67-2C90-414B-A21F-D9BE0A1E80F6";
    [NetHandler dataWithUrl:str complant:^(NSData *data) {
        
        self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@",self.dic);
        NSArray *arr = [self.dic objectForKey:@"data"];
        for (NSDictionary *dict in arr) {
            KindModel *kind = [[KindModel alloc]init];
            [kind setValuesForKeysWithDictionary:dict];
            [self.arr addObject:kind];
        }
        [self.collectionView reloadData];
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
        return self.arr.count -1;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    KindModel *kind = [self.arr objectAtIndex:indexPath.item];

        KindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kind" forIndexPath:indexPath];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:kind.classify_url]];
        cell.label.text = kind.classify_title;
        return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"%@",@"点击");
    DetailsViewController *detailsV = [[DetailsViewController alloc]init];
    [self.navigationController pushViewController:detailsV animated:YES];
    KindModel *kind = [self.arr objectAtIndex:indexPath.row];
    detailsV.listId = kind.classify_id;
    NSLog(@"%@",kind.classify_id);

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
